package com.rubiksillusions;

public class OrientationOfLastLayerCornersAlgorithm extends Algorithm {
	public String execute(RubiksCube rubiksCube) {
		for (int j = 0; j < 10; j++) {
			if (!rubiksCube.testStatus("wwwwwwwwwyyyyyyyyygggggggggbbbbbbbbbooooooooorrrrrrrrr")) {
				for (int i = 0; i < 4; i++) {
					if (!(isWellPositionedAndOriented(0, rubiksCube) && isWellPositionedAndOriented(6, rubiksCube))) {
						rubiksCube.executeMovesList("Z ", true);
					}
				}
				for (int i = 0; i < 4; i++) {
					if (!isWellPositionedAndOriented(0, rubiksCube)) {
						rubiksCube.executeMovesList("Z ", true);
					}
				}
				rubiksCube.executeMovesList("R U2 R' U' R U' R' L' U2 L U L' U L ", true);
			}
		}
		return rubiksCube.getExecutedMovesList(true);
	}
	
	private boolean isWellPositionedAndOriented(int cubelet, RubiksCube rubiksCube) {
		if (cubelet == 0) {
			if (rubiksCube.testStatus("....b.b........................o.o....y.y.............")) return true;
		} else if (cubelet == 2) {
			if (rubiksCube.testStatus("....b...b..................o...o.................y.y..")) return true;
		} else if (cubelet == 6) {
			if (rubiksCube.testStatus("b...b.............o...o.............y...y.............")) return true;
		} else if (cubelet == 8) {
			if (rubiksCube.testStatus("..b.b.................o.o....................y...y....")) return true;
		}
		
		return false;
	}
}
